﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ASoft.Service;

namespace ASoft.WPF
{
    public class WPFUtility
    {
        static WPFUtility() { }

        public static void TransFromDataToViewModel(object data, object viewModel)
        {
            TransData(data, viewModel, true);
        }

        public static void TransFromViewModelToData(object viewModel, object data)
        {
            TransData(data, viewModel, false);
        }

        static void TransData(object data, object viewModel, bool flag)
        {
            List<string> names;
            var dataType = data.GetType();
            var viewModelType = viewModel.GetType();
            names = dataType.GetProperties().Select(p => p.Name).ToList();
            names.ForEach(name =>
                {
                    var dataProperty = dataType.GetProperty(name);
                    var viewModelProperty = viewModelType.GetProperty(name);
                    if (dataProperty == null || viewModelProperty == null)
                    {
                        return;
                    }
                    if (flag)
                    {
                        viewModelProperty.SetValue(viewModel, dataProperty.GetValue(data, null), null);
                    }
                    else
                    {
                        dataProperty.SetValue(data, viewModelProperty.GetValue(viewModel, null), null);
                    }
                });
        }

        public static bool DataEqualsViewModel(object data, object viewModel)
        {
            List<string> names;
            var dataType = data.GetType();
            var viewModelType = viewModel.GetType();
            names = dataType.GetProperties().Select(p => p.Name).ToList();
            foreach (var name in names)
            {
                var dataProperty = dataType.GetProperty(name);
                var viewModelProperty = viewModelType.GetProperty(name);
                if (dataProperty == null || viewModelProperty == null)
                {
                    continue;
                }

                if (!object.Equals(dataProperty.GetValue(data, null), viewModelProperty.GetValue(viewModel, null)))
                {
                    return false;
                }
            }
            return true;
        }

        public const string DefaultTimeString = "00:00:00";



        public static void ExcelExport(List<object> objs, Dictionary<string, string> headerInfo)
        {

            Microsoft.Win32.SaveFileDialog dlg = new Microsoft.Win32.SaveFileDialog();
            dlg.FileName = "export"; // Default file name
            dlg.DefaultExt = ".xls"; // Default file extension
            dlg.Filter = "Excel documents (.xls)|*.xls"; // Filter files by extension

            // Show save file dialog box
            Nullable<bool> result = dlg.ShowDialog();

            // Process save file dialog box results
            if (result == true)
            {
                ExcelService service = new ExcelService();
                service.Export(objs, headerInfo, dlg.FileName);
            }
        }
    }
}
